package cn.bdqn.mh.config;

import cn.bdqn.mh.interceptor.MyInterceptor;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 创建人:  zhang
 * 创建日期: 2020/7/17 09:42
 * 此类用来扩展springmvc的配置信息;即保留了springboot的默认配置，有可以自己再定制
 */
@Data
@ConfigurationProperties(prefix = "file")
@Component
public class MvcConfigurer implements WebMvcConfigurer {
    private String uploadpath;//文件配置路径，值从yml中获取

    /**
     * 完整资源配置映射，上传的文件路径和虚拟路径之间的映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/upload/**")
                .addResourceLocations("file:///"+uploadpath);
    }
    //还可以继续配置拦截器

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        MyInterceptor myInterceptor=new MyInterceptor();
        registry.addInterceptor(myInterceptor) //注册拦截器
                .addPathPatterns("/test/**") //拦截路径
                .excludePathPatterns("/test/test4"); //排除路径，不起作用
    }

    //还可以配置ViewControlls
}
